<?php

namespace eu\freeplace\core;

/**
 * Caching based on Zend Cache.
 *
 * @author Elmar Dott
 * @version 1.0
 */
class Cache {
    
    const CLASS_NAME = "Cache";

    //Singelton Pattern =============================================
    static private $instance = null;
    static public function getInstance() {
         if (null === self::$instance) {
             self::$instance = new self;
         }
         return self::$instance;
    }
    /** Private Constructor implements a Singelton. */
    private function __construct() {
        global $logger;
        $logger->log("Calling instance of ".self::CLASS_NAME."()", "NOTICE"); }
    #================================================================

    //TODO: Cache

   // Calculate the Cache Time for the content.     
   private function getCacheTime() {
        
        //todo: get Configuration
        $time = "23:55";
        
        //Fallback
        if($time == "") $time = "00:01";
        
        // regex match 00:01 - 23:59
        if(preg_match("/^(0[0-9]|1[0-9]|2[0-3]):0[1-9]|[1-5][0-9]$/", $time) != 0) {
            
            $split_config_time = explode(":", $time);
            
            $config_time = ($split_config_time[0]*3600) + ($split_config_time[1]*60);            
            $curr_time   = (date('H')*3600) + (date('i')*60);     
            
            if($config_time > $curr_time) $time = $config_time - $curr_time; 
                else $time = "1";
        }
        
        global $siteLogger;
        $siteLogger->log(self::CLASS_NAME."().getCacheTime() - Cache is for ".$time." Milliseconds active.", "debug");
        
        return $time;
   }
   
}//CLASS

?>